package com.example.planetexploder.module.menu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;

import android.widget.LinearLayout;
import android.widget.ListView;
import com.example.planetexploder.adapter.MyAdapter_Menu;
import com.example.planetexploder.R;
import com.example.planetexploder.bean.otherdata.MenuItem;

import java.util.ArrayList;

public class View_Menu extends AppCompatActivity{

    private ArrayList<MenuItem> list;
    private Presenter_Menu presenter;
    private ListView mListView;//ListView控件
    private MyAdapter_Menu mAdapter;
    private int currentNum = -1;
    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewmenu);
        presenter = new Presenter_Menu(getApplicationContext());
        list =presenter.create_Menu();


        final CheckBox one = findViewById(R.id.cb_1);
        final CheckBox two = findViewById(R.id.cb_2);
        final CheckBox three = findViewById(R.id.cb_3);
        final CheckBox four = findViewById(R.id.cb_4);

        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                one.setChecked(true);
                two.setChecked(false);
                three.setChecked(false);
                four.setChecked(false);
            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                one.setChecked(false);
                two.setChecked(true);
                three.setChecked(false);
                four.setChecked(false);
            }
        });
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                one.setChecked(false);
                two.setChecked(false);
                three.setChecked(true);
                four.setChecked(false);
            }
        });
        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                one.setChecked(false);
                two.setChecked(false);
                three.setChecked(false);
                four.setChecked(true);
            }
        });

        layout=findViewById(R.id.menu_layout);

        //添加选择窗口范围监听可以优先获取触点，即不再执行onTouchEvent()函数，点击其他地方时执行onTouchEvent()函数销毁Activity
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });


        mListView = findViewById(R.id.MenuListView);
        mAdapter = new MyAdapter_Menu(View_Menu.this, list);
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (MenuItem menuItem : list) { //遍历list集合中的数据
                    menuItem.setChecked(false);//全部设为未选中
                }
                if (currentNum == -1) { //选中情况分析
                    list.get(position).setChecked(true);
                    currentNum = position;
                } else if (currentNum == position) { //同一个item选中变未选中
                    for (MenuItem menuItem : list) {
                        menuItem.setChecked(false);
                    }
                    currentNum = -1;
                } else if (currentNum != position) { //不是同一个item选中当前的，去除上一个选中的
                    for (MenuItem menuItem : list) {
                        menuItem.setChecked(false);
                    }
                    list.get(position).setChecked(true);
                    currentNum = position;
                }
                mAdapter.notifyDataSetChanged();//刷新adapter
            }
        });
    }

    /**
     * 功能：实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
     * 修改日期：2019.10.19
     */
    @Override
    public boolean onTouchEvent(MotionEvent event){
        finish();
        return true;
    }
}
